package hrhq.cntvcn.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.apache.solr.client.solrj.SolrServerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import hrhq.cntvcn.pojo.HrhqImage;
import hrhq.cntvcn.pojo.SearchResult;
import hrhq.cntvcn.service.SearchImageService;
/**
 * 测试solrcloud工程的web工程
 * @author Administrator
 *
 */
@Controller
@RequestMapping({"/search"})
public class SearchImageController
{
  private static Logger logger = Logger.getLogger(SearchImageController.class);
  /**
   * 拦截首页访问
   * 跳转测试方法
   * @param req
   * @param resp
   * @throws Exception
   */
  @RequestMapping({"/Image/index"})
  public void index(HttpServletRequest req, HttpServletResponse resp)
    throws Exception
  {
    resp.sendRedirect("http://localhost:8081/search/Image/byTitle");
  }
  
  
  @Autowired
  private SearchImageService siService;
  /**
   * 根据图库标题关键词进行搜索
   * @param titleKey 用户输入的图库标题关键词
   * @param rows 每页展示的结果数
   * @param currPage 当前页码
   * @param model
   * @return searchTest 搜索显示页面
   */
  @RequestMapping({"Image/byTitle"})
  public String searchByImageTitle(String titleKey,@RequestParam(defaultValue="20")Integer rows,@RequestParam(defaultValue="1")Integer currPage,Model model) {
	  long start = System.currentTimeMillis();
	  try {
		SearchResult<HrhqImage> searchResult = siService.searchByImageTitle("创意生活",rows,currPage);
		model.addAttribute("searchResult", searchResult);
	} catch (SolrServerException e) {
		e.printStackTrace();
	}
	  long end = System.currentTimeMillis();
	  System.out.println("搜索用时："+(end - start));
	  return "searchTestImage";
  }
}